package com.tuling.cloud.study.user.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.tuling.cloud.study.user.entity.User;

@RestController
public class OrderController {
	private static final Logger logger = LoggerFactory.getLogger(OrderController.class);
	@Autowired
	private RestTemplate restTemplate;

	@HystrixCommand(fallbackMethod = "findByIdFallback", //降级回调方法
					groupKey = "orderUserGroup",//线程租
					threadPoolKey = "orderUserIdThreadPool", //线程池名称
					threadPoolProperties = {//线程属性
	        			@HystrixProperty(name = "coreSize", value = "2"),//线程池大小
	        			@HystrixProperty(name = "maxQueueSize", value = "3"),//排队等待数
	        			@HystrixProperty(name = "queueSizeRejectionThreshold", value = "2")
			    //maxQueueSize和queueSizeRejectionThreshold属性,谁小执行谁
	})
	@GetMapping("/user/{id}")
	public User findById(@PathVariable Long id) {
		logger.info("================请求用户中心接口，用户id:" + id + "==============");
		return restTemplate.getForObject("http://microservice-provider-user/" + id, User.class);
	}

	public User findByIdFallback(Long id) {
		User user = new User();
		user.setId(-1L);
		user.setName("默认用户");
		return user;
	}
	
}
